﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Windows.Media;
using System.Text.RegularExpressions;
using System.Windows.Media.Imaging;
using DailyStrips.Utilities;
using DailyStrips.Model.ImageProvideStrategies;
using DailyComics.Properties;


namespace DailyStrips.Model
{
  public class ImageProvider
  {
    private string remotePath;
    private string localPath;

    private SizeRestrictedCache<DateTime, ImageSource> cache;

    private IImageProviderStrategy localStorage;
    private IImageProviderStrategy WebStorage;

    public ImageProvider()
    {
      remotePath = "http://wulffmorgenthaler.dk";
      localPath = "wulffmorgenthaler";

      cache = new SizeRestrictedCache<DateTime, ImageSource>(Settings.Default.MemoryCacheSize);

      localStorage = new LocalStorageImageProviderStrategy(localPath);
      WebStorage = new WebPageImageProviderStrategy(remotePath);
    }

    public ImageSource GetImage(DateTime date)
    {
      //Create some strategy selector to select strategy


      if (cache.ContainsKey(date)) return cache[date];

      if (localStorage.IsImageAvailable(date)) 
      {
        cache.Add(date, localStorage.GetImage(date));
        return cache[date];
      }

      if (WebStorage.IsImageAvailable(date))
      {
        cache.Add(date, WebStorage.GetImage(date));
        return cache[date];
      }

      return null; //find som "Image is not available" image 
    }

  }
}
